package com.showde.shoppingmall;

import java.util.HashMap;

import android.app.Application;
import android.content.Intent;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.kim.androidbase.log.LogUtil;
import com.kim.net.netState.ConnectionReceiver;
import com.kim.net.netState.ConnectionReceiver.NetStateChangeCallback;
import com.kim.net.netState.NetStateUtil;
import com.showde.shoppingmall.business.logic.CallbackData;
import com.showde.shoppingmall.business.logic.LoginLogic;
import com.showde.shoppingmall.business.logic.UICallbackInterface;
import com.showde.shoppingmall.db.UserDBCommand;
import com.showde.shoppingmall.domain.User;
import com.showde.shoppingmall.service.FaceImageFetchService;
import com.showde.shoppingmall.utils.LogRecorder;

public class ShoppingMallApplication extends Application {

	private static final String TAG = "ShoppingMallApplication";

	/**
	 * 是否开发模式
	 */
	public static final boolean DEVMode = true;

	public static String packageName = null;

	public static DatabaseErrorHandler databaseErrorHandler;

	public static UserSession userSession = new UserSession();

	public interface RefreshPage {
		void onFresh();
	}

	private static HashMap<String, RefreshPage> refreshPages = new HashMap<String, RefreshPage>();

	@Override
	public void onCreate() {
		super.onCreate();
		packageName = getPackageName();

		// 设置日志存储位置
		LogUtil.setLogPath(packageName);

		databaseErrorHandler = new DatabaseErrorHandler() {

			@Override
			public void onCorruption(SQLiteDatabase dbObj) {
				LogRecorder.e(ShoppingMallApplication.class.getName(),
						"Database onCorruption");
			}
		};

		// 监听网络状态
		ConnectionReceiver.addNetChangeListener(new NetStateChangeCallback() {

			@Override
			public void onChange(int netState, int netType) {
				if (netState == NetStateUtil.CONNECT
						&& netType == NetStateUtil.WIFI) {
					Intent service = new Intent();
					service.setAction(FaceImageFetchService.ACTION_FETCH);
					startService(service);
				}
			}
		});
		UserDBCommand userDBCommand = new UserDBCommand(this);
		User user = userDBCommand.queryCurrentUser();
		if (user != null) {
			// 静默登录
			LoginLogic.login(this, user.getUserAccount(),
					user.getUserPassword(), new UICallbackInterface() {

						@Override
						public void onSuccess(CallbackData data) {
							Log.d(TAG, "login silent  success...");
						}

						@Override
						public void onError(String errorMsg) {
							Log.d(TAG, "login silent  fail:" + errorMsg);
						}
					});

		}
	}

	public static void registerFresh(String pageKey, RefreshPage page) {
		refreshPages.put(pageKey, page);
	}

	public static void refresh(String key) {
		try {
			refreshPages.get(key).onFresh();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static User getSessionUser() {
		return userSession.getSessionUser();
	}
}
